<?php
#
# VERSION
#
define('CAPTOR_VERSION', '1.0');
define('CAPTOR_AUTHOR', 'Petko D. (pdp) Petkov');
define('CAPTOR_AUTHOR_HOMEPAGE', 'http://www.gnucitizen.org/about/pdp');

#
# HELPERS
#
function captor_session_is($code, $name='default') {
	$_code = $_SESSION['captor.code.'.$name];

	unset($_SESSION['captor.code.'.$name]);

	return $code == $_code;
}

function captor_generate_code($characters) {
	$possible = '23456789bcdfghjkmnpqrstvwxyz';
	$code = '';
	$i = 0;

	while ($i < $characters) { 
		$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
		$i++;
	}

	return $code;
}

#
# MAIN
#
# $bgcolor=array(255, 255, 255), $textcolor=array(20, 40, 100), $noisecolor=array(100, 120, 180)
function captor_generate_captcha($font, $width=120, $height=40, $characters=6, $bgcolor=array(255, 255, 255), $textcolor=array(30, 30, 30), $noisecolor=array(0, 0, 0)) {
	$code = captor_generate_code($characters);

	# $font_size = $height * 0.75;
	$font_size = $height * 0.55;

	if (!($image = @imagecreate($width, $height))) {
		return false;
	}

	$background_color = imagecolorallocate($image, $bgcolor[0], $bgcolor[1], $bgcolor[2]);
	$text_color = imagecolorallocate($image, $textcolor[0], $textcolor[1], $textcolor[2]);
	$noise_color = imagecolorallocate($image, $noisecolor[0], $noisecolor[1], $noisecolor[2]);

	for($i=0; $i < ($width * $height)/3; $i++) {
		imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
	}

	for($i=0; $i < ($width * $height)/150; $i++) {
		imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
	}

	if (!($textbox = imagettfbbox($font_size, 0, $font, $code))) {
		return false;
	}

	$x = ($width - $textbox[4])/2;
	$y = ($height - $textbox[5])/2;

	if (!imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code)) {
		return false;
	}

	header('Content-Type: image/jpeg');

	imagejpeg($image);
	imagedestroy($image);

	return $code;
}

function captor_generate_captcha_session($font, $name='default', $width=120, $height=40, $characters=6, $bgcolor=array(255, 255, 255), $textcolor=array(30, 30, 30), $noisecolor=array(0, 0, 0)) {
	return ($_SESSION['captor.code.'.$name] = captor_generate_captcha($font, $width, $height, $characters, $bgcolor, $textcolor, $noisecolor));
}

/*
 * File: CaptchaSecurityImages.php
 * Author: Simon Jarvis
 * Copyright: 2006 Simon Jarvis
 * Date: 03/08/06
 * Updated: 07/02/07
 * Requirements: PHP 4/5 with GD and FreeType libraries
 * Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
 * 
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details: 
 * http://www.gnu.org/licenses/gpl.html
 *
 */
